var _tools = require("../../utils/tools");

function _toConsumableArray(t) {
    if (Array.isArray(t)) {
        for (var a = 0, e = Array(t.length); a < t.length; a++) e[a] = t[a];
        return e;
    }
    return Array.from(t);
}

var g, app = getApp(), page = app.page, request = app.util.request, route = app.route, toast = app.toast;

function wendaPay(t) {
    request({
        url: "QuestionOrderPay",
        data: {
            order_id: t,
            user_id: app.userInfo.id || wx.getStorageSync("userInfo").id
        },
        success: function(t) {
            t.appId ? (0, _tools.wxPay)({
                data: t,
                success: function(t) {
                    app.item = {
                        title: "发布成功",
                        content: "已发布,请耐心等待" + g.data.system.law_custom + "回复",
                        img: "pay_ok",
                        button: [ {
                            title: "返回首页",
                            url: "loading",
                            type: "reLaunch"
                        }, {
                            title: "返回上一页",
                            delta: "loading",
                            type: "navigateBack"
                        } ]
                    }, route({
                        type: "redirect",
                        url: "result"
                    });
                }
            }) : toast("错误:" + t);
        }
    });
}

page({
    data: {
        imgList: [],
        curr: 0
    },
    onLoad: function(t) {
        g = this, request({
            url: "LawType",
            showLoading: !1,
            success: function(t) {
                return g.setData({
                    type: t
                });
            }
        });
    },
    onPullDownRefresh: function() {},
    toDomain: function() {
        route({
            type: "navigate",
            url: "wenda_domain"
        });
    },
    radio: function(t) {
        g.setData({
            radio: !g.data.radio
        });
    },
    html_page: function() {
        app.centent = g.data.system.wenda.content, route({
            type: "navigate",
            url: "html_page",
            params: {
                title: "发布协议"
            }
        });
    },
    chooseImage: function() {
        wx.chooseImage({
            count: 4,
            success: function(t) {
                t = t.tempFilePaths;
                return g.setData({
                    imgList: [].concat(_toConsumableArray(g.data.imgList), _toConsumableArray(t))
                });
            }
        });
    },
    viewImage: function(t) {
        wx.previewImage({
            urls: g.data.imgList,
            count: t.currentTarget.dataset.index
        });
    },
    delImage: function(t) {
        var a = g.data.imgList;
        a.splice(t.currentTarget.dataset.index, 1), g.setData({
            imgList: a
        });
    },
    pickerChange: function(t) {
        g.setData({
            curr: t.detail.value
        });
    },
    addSubmit: function(t) {
        var a = (t = t.detail.value).title, e = t.price, i = t.content, n = t.is_niming ? 1 : 0;
        return a ? e ? parseFloat(e) < parseFloat(g.data.system.wenda.min_price) ? toast("请填写正确的金额") : i ? g.data.radio ? g.data.order_id ? wendaPay(g.data.order_id) : void request({
            url: "ContentSafe",
            data: {
                content: i
            },
            showLoading: !1,
            success: function(t) {
                wx.showLoading({
                    title: "上传图片中...",
                    mask: !0
                }), (0, _tools.uploadImg)(g.data.imgList, function(t) {
                    wx.showLoading({
                        title: "提交中...",
                        mask: !0
                    }), request({
                        url: "QuestionPublish",
                        method: "POST",
                        data: {
                            title: a,
                            type_id: g.data.type[g.data.curr].id,
                            imglist: t,
                            content: i,
                            price: e,
                            user_id: app.userInfo.id || wx.getStorageSync("userInfo").id,
                            is_niming: n
                        },
                        showLoading: !1,
                        success: function(t) {
                            wx.hideLoading()
                            if(e == 0){
                                app.item = {
                                    title: "发布成功",
                                    content: "已发布,请耐心等待" + g.data.system.law_custom + "回复",
                                    img: "pay_ok",
                                    button: [ {
                                        title: "返回首页",
                                        url: "loading",
                                        type: "reLaunch"
                                    }, {
                                        title: "返回上一页",
                                        delta: "loading",
                                        type: "navigateBack"
                                    } ]
                                }, route({
                                    type: "redirect",
                                    url: "result"
                                });
                            } else {
                                // g.setData({
                                //     order_id: t
                                // })
                                wendaPay(t);
                            }
                            
                        }
                    });
                });
            }
        }) : toast("请阅读并勾选用户发布协议") : toast("请填写问题描述") : toast("请填写" + (this.data.system.wenda.sj_title || "赏金") + "金额") : toast("请填写标题");
    }
});